import { wechatPay } from "@/api/modules/wechatPay"
import { getUserinfo } from "../userinfo/userinfo"

const orderTypeDic = {
  'Hotel': 1,//酒店民宿 
  'Sight': 2,//景点 
  'Product': 3,//商品 
  'Restaurant': 4,//餐饮 
}

/**
 * 去支付
 * @param { 'Hotel' | 'Sight' | 'Product' | 'Restaurant' } orderType 商铺类型枚举 酒店民宿 Hotel = 1 景点 Sight = 2 商品 Product = 3 餐饮 Restaurant = 4
 * @param {Number} orderNum 订单号
 */
export const toWxPay = (orderType, orderNum) => {
  wx.showLoading({
    title: '加载中...',
  })
  const { openId } = getUserinfo()
  return new Promise((resolve, reject) => {
    wechatPay.pay({
      orderType: orderTypeDic[orderType],
      orderNum,
      openId
    }).then(({ result }) => {
      wx.requestPayment({
        ...result.miniProgramParam,
        success: (result) => {
          resolve()
          console.log('success-result', result);
        },
        fail: ({ errMsg }) => {
          reject(errMsg)
          if (errMsg === 'requestPayment:fail cancel') {
            wx.showToast({
              title: "用户取消支付！",
              icon: "none"
            })
          } else {
            wx.showToast({
              title: '支付失败：' + errMsg,
              icon: "none"
            })
            console.log('errMsg', errMsg);
          }
          console.log('fail-result', result);
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    }).catch((result) => {
      reject(result)
      wx.hideLoading()
    })
  })
}

/**
 * 订单合并支付（商城订单掉此接口）
 * @param { 'Hotel' | 'Sight' | 'Product' | 'Restaurant' } orderType 商铺类型枚举 酒店民宿 Hotel = 1 景点 Sight = 2 商品 Product = 3 餐饮 Restaurant = 4
 * @param {Array} orderNum 订单号
 */
export const toWxPayMore = (orderType, orderNum) => {
  if (!Array.isArray(orderNum)) {
    orderNum = [orderNum]
  }
  wx.showLoading({
    title: '加载中...',
  })
  const { openId } = getUserinfo()
  return new Promise((resolve, reject) => {
    wechatPay.payMore({
      orderNumList: orderNum,
      openId
    }).then(({ result }) => {
      wx.requestPayment({
        ...result.miniProgramParam,
        success: (result) => {
          resolve()
          console.log('success-result', result);
        },
        fail: ({ errMsg }) => {
          reject(errMsg)
          if (errMsg === 'requestPayment:fail cancel') {
            wx.showToast({
              title: "用户取消支付！",
              icon: "none"
            })
          } else {
            wx.showToast({
              title: '支付失败：' + errMsg,
              icon: "none"
            })
          }
          console.log('fail-result', result);
        },
        complete: () => {
          wx.hideLoading()
        }
      })

    }).catch((result) => {
      wx.hideLoading()
      reject(result)
    })
  })
}

/**
 * 取消订单(关闭支付) 待支付才可以取消
 * @param {Number} orderNum 订单号
 * @param { 'Hotel' | 'Sight' | 'Product' | 'Restaurant' } orderType 商铺类型枚举 酒店民宿 Hotel = 1 景点 Sight = 2 商品 Product = 3 餐饮 Restaurant = 4
 */
export const toWxPayClose = (orderNum, orderType) => {
  const { openId } = getUserinfo()
  return new Promise((resolve, reject) => {
    wechatPay.close({
      orderType: orderTypeDic[orderType],
      orderNum,
      openId
    }).then(({ result }) => {
      resolve(result)
    }).catch((result) => {
      reject(result)
    })
  })
}
